package com.lanou.userLogin.user.web.servlet;

import com.lanou.userLogin.user.domain.User;
import com.lanou.userLogin.user.service.UserRegisterService;
//import com.lanou.userLogin.user.service.UserRegisterServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/userRegisterTwoServlet")
public class UserRegisterTwoServlet extends HttpServlet {
    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取form表单数据
        String uid = req.getParameter("uid");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String email = req.getParameter("email");
        String code = req.getParameter("code");
        User user = new User();
        user.setUid(uid);
        user.setUsername(username);
        user.setPassword(password);
        user.setEmail(email);
        user.setCode(code);

        //判断
        UserRegisterService ur = new UserRegisterService();
        boolean register = ur.register(user);
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter pw = resp.getWriter();
        if (register == true) {
            System.out.println("成功");
            resp.setStatus(302);
            pw.write("注册成功，3秒后跳转到登录页面");
            resp.setHeader("refresh","3;/jsps/user/login.jsp");
        } else {
            System.out.println("失败");
            resp.setStatus(302);
            pw.write("注册失败，3秒后回到注册页面");
            resp.setHeader("refresh","3;/jsps/user/regist.jsp");
        }
    }
}
